<?php
/**
 * SpiderController.php class file.
 * @author wudg <544038230@qq.com>
 * @date 2020/11/2- 2:42 下午
 */

namespace console\controllers;

use yii\console\Controller;
use api\services\OnsaleService;
use yii\helpers\FileHelper;
use common\drivers\queue\Manager;
use Yii;

class OnsaleController extends Controller
{


    //执行计划
    public function actionTask()
    {
        $ret = (new OnsaleService())->task();
        $this->_writeLog('任务投递成功');
    }

    public function actionRediscrawler()
    {
        $redisCrawler = Yii::$app->redis->get('super_pdd_crawler_work_time_0');//redis执行时间
        if((time()-$redisCrawler)>=20){
            $test = "ps -ef|grep pdd_crawler|grep -v grep|cut -c 9-15|xargs kill -9";
            $fp = popen($test, "r");  //popen打一个进程通道

            while (!feof($fp)) {      //从通道里面取得东西
                $out = fgets($fp, 4096);
                echo $out;         //打印出来
            }
            pclose($fp);

            $this->_writeLog('ps -ef|grep pdd_crawler|grep -v grep|cut -c 9-15|xargs kill -9任务执行成功');
        }
    }

    public function actionRediscloud()
    {
        $redisCrawler = Yii::$app->redis->get('super_pdd_cloud_work_time_0');//redis执行时间
        if((time()-$redisCrawler)>=20){
            $test = "ps -ef|grep pdd_cloud|grep -v grep|cut -c 9-15|xargs kill -9";
            $fp = popen($test, "r");  //popen打一个进程通道

            while (!feof($fp)) {      //从通道里面取得东西
                $out = fgets($fp, 4096);
                echo $out;         //打印出来
            }
            pclose($fp);

            $this->_writeLog('ps -ef|grep pdd_cloud|grep -v grep|cut -c 9-15|xargs kill -9任务执行成功');
        }

        exit();
    }

    public function actionRedispush()
    {
        $redisCrawler = Yii::$app->redis->get('super_pdd_push_work_time_0');//redis执行时间
        if((time()-$redisCrawler)>=20){
            $test = "ps -ef|grep pdd_push|grep -v grep|cut -c 9-15|xargs kill -9";
            $fp = popen($test, "r");  //popen打一个进程通道

            while (!feof($fp)) {      //从通道里面取得东西
                $out = fgets($fp, 4096);
                echo $out;         //打印出来
            }
            pclose($fp);

            $this->_writeLog('ps -ef|grep pdd_push|grep -v grep|cut -c 9-15|xargs kill -9任务执行成功');
        }

        exit();
    }

    /**
     * 文件日志记录
     * @param $msg
     * @throws \yii\base\Exception
     */
    private function _writeLog($msg)
    {

        $path = Yii::getAlias('@runtime/api/redis/');

        //目录如果不存就先创建目录
        if (!is_dir($path)) {
            FileHelper::createDirectory($path);
        }

        $cp = fopen($path . 'sync-items-' . date('Ymd') . '.log', "a+");
        $txt = "【" . date('Y-m-d H:i:s', time()) . "】---" . $msg . "\r\n";
        fwrite($cp, $txt);
        fclose($cp);
    }
}
